package documents

import (
	"bytes"
	"fmt"
	"github.com/xuri/excelize/v2"
	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"
)

// XLS Microsoft Office Excel
// XLT Microsoft Office Excel Template
// XLSX Microsoft Office EXCEL 2007/2010/2013/2016/2019
// XLTX Microsoft Office EXCEL 2007/2010/2013/2016/2019 Template
// XLSM 启用宏的工作簿
// XLTM 启用宏的工作簿模板
// XLAM 启用宏的加载项工作簿

// go get github.com/xuri/excelize/v2

func Create() {
	f := excelize.NewFile()
	defer func() {
		if err := f.Close(); err != nil {
			panic(err)
		}
	}()

	index, err := f.NewSheet("Sheet2")
	if err != nil {
		panic(err)
	}

	f.SetActiveSheet(index)

	err = f.SetCellValue("Sheet2", "A2", "Hello world.")
	if err != nil {
		panic(err)
	}

	f.SetColWidth("Sheet1", "A", "C", 20)
	f.SetRowHeight("Sheet1", 1, 30)

	exp := "[$-380A]dddd\\,\\ dd\" de \"mmmm\" de \"yyyy;@"
	style, err := f.NewStyle(&excelize.Style{CustomNumFmt: &exp})
	if err != nil {
		panic(err)
	}
	fmt.Println(style)

	headStyleID, err := f.NewStyle(
		&excelize.Style{
			Font: &excelize.Font{
				Bold:   true,
				Family: "arial",
				Size:   16,
				Color:  "#333333",
			},
			Alignment: &excelize.Alignment{
				Vertical:   "center",
				Horizontal: "center",
			},
		})
	if err != nil {
		panic(err)
	}

	err = f.SetCellStyle("Sheet1", "A1", "C1", headStyleID)
	if err != nil {
		panic(err)
	}
	// err = f.SetCellStyle("Sheet1", "A2", "C2", rowStyleID)
	// if err != nil {
	// 	panic(err)
	// }

	if err := f.SaveAs("Book1.xlsx"); err != nil {
		panic(err)
	}
}
func Read() {
	f, err := excelize.OpenFile("Book1.xlsx")
	if err != nil {
		panic(err)
	}
	defer func() {
		if err := f.Close(); err != nil {
			panic(err)
		}
	}()

	cell, err := f.GetCellValue("Sheet1", "B2")
	if err != nil {
		panic(err)
	}
	fmt.Println(cell)

	rows, err := f.GetRows("Sheet1")
	if err != nil {
		panic(err)
	}

	for _, row := range rows {
		for _, cell := range row {
			fmt.Print(cell, "\t")
		}
		fmt.Println()
	}
}
func abc() {
	f := excelize.NewFile()
	defer func() {
		if err := f.Close(); err != nil {
			panic(err)
		}
	}()

	for idx, row := range [][]interface{}{
		{nil, "Apple", "Orange", "Pear"},
		{"Small", 2, 3, 3},
		{"Normal", 5, 2, 4},
		{"Large", 6, 7, 8},
	} {
		cell, err := excelize.CoordinatesToCellName(1, idx+1)
		if err != nil {
			panic(err)
		}

		err = f.SetSheetRow("Sheet1", cell, &row)
		if err != nil {
			panic(err)
		}
	}

	err := f.AddChart("Sheet1", "E1", &excelize.Chart{
		Type: excelize.Col3DClustered,
		Series: []excelize.ChartSeries{
			{
				Name:       "Sheet1!$A$2",
				Categories: "Sheet1!$B$1:$D$1",
				Values:     "Sheet1!$B$2:$D$2",
			},
			{
				Name:       "Sheet1!$A$3",
				Categories: "Sheet1!$B$1:$D$1",
				Values:     "Sheet1!$B$3:$D$3",
			},
			{
				Name:       "Sheet1!$A$4",
				Categories: "Sheet1!$B$1:$D$1",
				Values:     "Sheet1!$B$4:$D$4",
			}},
		Title: excelize.ChartTitle{
			Name: "Fruit 3D Clustered Column Chart",
		},
	})
	if err != nil {
		panic(err)
	}

	if err := f.SaveAs("Book1.xlsx"); err != nil {
		panic(err)
	}
}
func Image() *bytes.Buffer {
	f, err := excelize.OpenFile("Book1.xlsx")
	if err != nil {
		panic(err)
	}
	defer func() {
		if err := f.Close(); err != nil {
			panic(err)
		}
	}()

	if err := f.AddPicture("Sheet1", "A2", "image.png", nil); err != nil {
		panic(err)
	}

	if err := f.AddPicture("Sheet1", "D2", "image.jpg", &excelize.GraphicOptions{ScaleX: 0.5, ScaleY: 0.5}); err != nil {
		panic(err)
	}

	enable, disable := true, false
	if err := f.AddPicture("Sheet1", "H2", "image.gif", &excelize.GraphicOptions{
		PrintObject:     &enable,
		LockAspectRatio: false,
		OffsetX:         15,
		OffsetY:         10,
		Locked:          &disable,
	}); err != nil {
		panic(err)
	}

	buffer, err := f.WriteToBuffer()
	if err != nil {
		panic(err)
	}

	return buffer
	// easyResponse.New(r).
	// 	SetHeader("Content-Type", "application/force-download").
	// 	SetHeader("Accept-Ranges", "bytes").
	// 	SetHeader("Content-Disposition", fmt.Sprintf(`attachment;filename=%s`, url.QueryEscape("1.xlsx"))).
	// 	WriteBinary(http.StatusOK, buffer.Bytes())

	// http.ServeContent(r.Response.Writer.RawWriter(), r.Request, "1.txt", time.Time{}, buffer)
}
